//
//  FDSureOrderBottomViewXin.m
//  FD
//
//  Created by xiaofang wang on 2019/1/10.
//  Copyright © 2019年 ZhiYuan Network. All rights reserved.
//

#import "FDSureOrderBottomViewXin.h"
@interface FDSureOrderBottomViewXin()
/**1.购物车按钮btn*/
@property (nonatomic, strong) UIButton *cartBtn;
/**2.红色的角标 暂时隐藏 10-20 */
@property (nonatomic, strong) UILabel *badgeLb;
/**3.价格lb 使用富文本*/
@property (nonatomic, strong) UILabel *priceLb;

//!!!!:2019-2-16隐藏同时要修改布局
/**原价 灰色 */
@property (nonatomic,  strong) UILabel *originPriceLb;

/**配送时间view*/
@property (nonatomic,  strong) UILabel *timeLb;


/**5.备注："另需配送费10元 | 支持自提
 1-10 在改回來
 */
@property (nonatomic, strong) UILabel *remarkLb;
/**6.去结算 按钮 11-27修改,请求到运费后:红色,可以点击; 反之灰色、不然点击*/
@property (nonatomic, strong) UIButton *jieSuanBtn;

/**2019-2-21价格*/
@property (nonatomic) CGFloat tempOriginPrice;


@end

@implementation FDSureOrderBottomViewXin
- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        self.backgroundColor = [UIColor whiteColor];
        [self setupView];
    }
    return self;
}

- (void)setupView {
    [self addSubview:self.cartBtn];
    //    [self addSubview:self.badgeLb];
    [self addSubview:self.priceLb];
    [self addSubview:self.remarkLb];
    
    [self addSubview:self.originPriceLb];
    self.originPriceLb.hidden = YES; //2019-2-16隐藏
    
    [self addSubview:self.timeLb];
    [self addSubview:self.jieSuanBtn];
}

#pragma mark - layout
- (void)layoutSubviews {
    [super layoutSubviews];
    
    //购物车按钮
    [self.cartBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.size.mas_equalTo(CGSizeMake(50, 50));
        make.left.equalTo(self.mas_left).offset(0);
        make.centerY.equalTo(self.mas_centerY);
    }];
    
    [self.priceLb mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.cartBtn.mas_right).offset(5);
        make.top.equalTo(self.mas_top).offset(5);
        make.height.mas_equalTo(20);
        make.width.greaterThanOrEqualTo(@40);
    }];
    
    [self.originPriceLb mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.priceLb.mas_right);
        make.top.equalTo(self.mas_top).offset(5);
        make.height.mas_equalTo(20);
        make.width.greaterThanOrEqualTo(@35);
    }];
    
    [self.timeLb mas_makeConstraints:^(MASConstraintMaker *make) {
//        make.left.equalTo(self.originPriceLb.mas_right);
        make.left.equalTo(self.priceLb.mas_right).offset(5); //2019-2-16 originPriceLb 隐藏
        
        make.top.equalTo(self.mas_top).offset(5);
        make.height.mas_equalTo(20);
    }];
    
    [self.remarkLb mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.cartBtn.mas_right).offset(5);
        make.top.equalTo(self.priceLb.mas_bottom);
        make.size.mas_equalTo(CGSizeMake(200, 30));
    }];
    [self.jieSuanBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.width.mas_equalTo(90);
        make.top.bottom.right.equalTo(self);
    }];
    //12-31 左图右文字
    [self.jieSuanBtn buttonWithTextImg];
}

#pragma mark - getter --- ↓↓↓↓↓↓↓↓↓
- (UIButton *)cartBtn {
    if (!_cartBtn) {
        _cartBtn = [UIButton buttonWithType:(UIButtonTypeCustom)];
        [_cartBtn setImage:[UIImage imageNamed:@"gwc.png"] forState:(UIControlStateNormal)];
    }
    return _cartBtn;
}

//圆形角标:(30,30)
- (UILabel *)badgeLb {
    if (!_badgeLb) {
        _badgeLb = [UILabel new];
        _badgeLb.textColor = [UIColor whiteColor];
        _badgeLb.backgroundColor = [UIColor redColor];
        _badgeLb.layer.masksToBounds = YES;
        _badgeLb.layer.cornerRadius = 10;
        _badgeLb.font = [UIFont systemFontOfSize:12.0];
        _badgeLb.text = [NSString stringWithFormat:@"5"];
        _badgeLb.textAlignment = NSTextAlignmentCenter;
    }
    return _badgeLb;
}
- (UILabel *)priceLb {
    if (!_priceLb) {
        _priceLb = [UILabel new];
        _priceLb.text = @"€ 0.0";
        _priceLb.font = [UIFont systemFontOfSize:16.0*kScaleFit];
        _priceLb.textColor = [UIColor redColor];
    }
    return _priceLb;
}

- (UILabel *)remarkLb {
    if (!_remarkLb) {
        _remarkLb = [UILabel new];
//        国际化
//        if ([NSString rb_ChineseLanguage]) {
//            _remarkLb.text = @"另需配送费:€ 0.0|支持自提";
//        }else {
//            _remarkLb.text = @"Freight:€ 0.0|collection";
//        }
        
        //2019-2-19
        if ([NSString rb_ChineseLanguage]) {
            _remarkLb.text = @"包含配送费:€ 0.0";
        }else {
            _remarkLb.text = @"Delivery Fee Included:€ 0.0";
        }
        
        //12-31
        //参考android
        _remarkLb.font = [UIFont systemFontOfSize:13.0];
    }
    return _remarkLb;
}

- (UIButton *)jieSuanBtn {
    if (!_jieSuanBtn) {
        _jieSuanBtn = [UIButton buttonWithType:(UIButtonTypeCustom)];
        [_jieSuanBtn setTitleColor:[UIColor whiteColor] forState:(UIControlStateNormal)];
        NSString *qjsStr = NSLocalizedString(Order_Settlement, @"去结算");
        
        [_jieSuanBtn setTitle:[NSString stringWithFormat:@"%@",qjsStr] forState:(UIControlStateNormal)];
        //        _jieSuanBtn.titleLabel.backgroundColor = [UIColor brownColor];
        _jieSuanBtn.titleLabel.font = [UIFont systemFontOfSize:15.0];
        _jieSuanBtn.backgroundColor = kMainRedColor;
        [_jieSuanBtn setImage:[UIImage imageNamed:@"leftArrow"] forState:(UIControlStateNormal)];
        [_jieSuanBtn addTarget:self action:@selector(clickJieSuanBtnAction:) forControlEvents:(UIControlEventTouchUpInside)];
    }
    return _jieSuanBtn;
}

- (UILabel *)originPriceLb {
    if (!_originPriceLb) {
        _originPriceLb = [UILabel new];
        _originPriceLb.textColor = kLightGrayTextColor;
        _originPriceLb.text = @"€0.0";
        _originPriceLb.font = [UIFont systemFontOfSize:13.0];
    }
    return _originPriceLb;
}

- (UILabel *)timeLb {
    if (!_timeLb) {
        _timeLb = [UILabel new];
        _timeLb.backgroundColor = rgba(170, 170, 170, 1.0);
        _timeLb.textColor = [UIColor whiteColor];
        _timeLb.font = [UIFont systemFontOfSize:10.0*kScaleFit];
        _timeLb.layer.cornerRadius = 5;
        _timeLb.text = @"10:0am~11:0pm";
        _timeLb.layer.masksToBounds = YES;
    }
    return _timeLb;
}


#pragma mark - 事件监听

/**去结算*/
- (void)clickJieSuanBtnAction:(UIButton *)btn {
    if (self.clickJieSuanBlock) {
        self.clickJieSuanBlock();
    }
}


/**3.更新总价*/
- (void)updateshopcartBottomViewTotalPrice:(CGFloat)totalPrice originPrice:(CGFloat)originPrice freight:(CGFloat)freight{
    NSString *priceStr = [NSString stringWithFormat:@"€ %.1f",totalPrice];
    self.priceLb.text = priceStr;
    
    self.tempOriginPrice = totalPrice; //2019-2-21
    
    if (originPrice <=0) {
        FDLog(@"2019-1-15如果传的是0就不处理。接口写的不好。先实现功能");
    }else {
//        self.originPriceLb.text = [NSString stringWithFormat:@"€%.1f",originPrice];
        self.originPriceLb.text = [NSString stringWithFormat:@"€%.1f",originPrice - freight];
    }
}

/**4.更新营业时间*/
- (void)updateYingYeShiJianBegin:(NSString *)begin end:(NSString *)end{
    
    //背景颜色红，文字颜色白
    self.timeLb.textColor = [UIColor whiteColor];
    self.timeLb.backgroundColor = kMainRedColor;
    
    self.timeLb.text = [NSString stringWithFormat:@"%@am~%@pm",begin,end];
}

/**5.更新运费 运费价格计算接口请求比较慢，客户端处理为：
 如果接口请求中，运费显示“计算中...”，
 同时确认订单按钮为灰色不可点击状态，接口请求完毕后恢复正常*/
- (void)updateFreightAndUpdateJieSuanBtnStateWithFreight:(CGFloat)freight {
    
    if (freight) { //请求到运费就显示 & 按钮可以点击
        //        国际化
        
        //        12-31 修改为 VIEW BASKET.
        //        if ([NSString rb_ChineseLanguage]) {
        //            _remarkLb.text = [NSString stringWithFormat:@"另需配送费€%.1f|支持自提",freight];
        //        }else {
        //            _remarkLb.text = [NSString stringWithFormat:@"Freight€%.1f|collection",freight];
        //        }
        
        
        
        //2019-2-19
        if ([NSString rb_ChineseLanguage]) {
            _remarkLb.text = [NSString stringWithFormat:@"包含配送费:€ %.1f",freight];
        }else {
            _remarkLb.text = [NSString stringWithFormat:@"Delivery Fee Included:€ %.1f",freight];
        }
        

        //2019-2-21 更新价格:总价-运费
        self.priceLb.text = [NSString stringWithFormat:@"€%.1f",self.tempOriginPrice + freight];
        
        
        //按钮可点击,红色
        [self updateBtnBgcEnable:YES];
    }else { //没有请求到运费。按钮不让点击
        //按钮不可点击,灰色
        [self updateBtnBgcEnable:NO];
    }
}

/**5-2为了应付上一个需求*/
- (void)updateBtnBgcEnable:(BOOL)enable {
    if (enable) {
        self.jieSuanBtn.backgroundColor = kMainRedColor; //灰色的
        self.jieSuanBtn.enabled = YES;//不让点
    }else {
        self.jieSuanBtn.backgroundColor = [UIColor lightGrayColor]; //灰色的
        self.jieSuanBtn.enabled = NO;//不让点
    }
}

@end
